package com.gjzhao.fmmall.service.juc;

import java.util.concurrent.TimeUnit;

/**
 * @Description: 设置守护线程
 * @Author: gjzhao
 * @Date Created in 2022-03-05 10:57 下午
 */
public class TestSetDaemonThread {
    public static void main(String[] args) {

        Thread thread = new Thread(() -> {

            System.out.println("--- 当前线程为:" + (Thread.currentThread().isDaemon() ? "守护线程 " : "用户线程"));

            try {
                TimeUnit.SECONDS.sleep(15);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "A线程");

        //1.设置当前线程为守护线程
        thread.setDaemon(true);

        //2.启动线程
        thread.start();

        //设置用户线程睡眠10s
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("--- main线程为:" + (Thread.currentThread().isDaemon() ? "守护线程 " : "用户线程"));
    }
}
